package 反射.T13;

import java.lang.reflect.Method;

public class T13 {

    public static void main(String[] args) throws Exception {

        Class<?> aClass = Class.forName("反射.T13.Student");
        Object obj = aClass.newInstance();
        invokeMethod(obj,"showInfo",new Object[]{"张三","北京"});
        invokeMethod(obj,"showInfo2",new Object[]{"张三"});
        invokeMethod(obj,"showInfo3",new Object[]{"张三","北京",18});

    }


    public static Object invokeMethod(Object obj,String methodName,Object[] args)throws Exception{

        Class<?> aClass = obj.getClass();

        Class[] cs = new Class[args.length];
        for (int i = 0; i < args.length; i++) {
            cs[i] = args[i].getClass();
        }

        Method method = aClass.getMethod(methodName, cs);

        Object rel = method.invoke(obj, args);

        return rel;
    }
}
